#include<main.h>


//（1）和（2）
void Input(STUDENT stu[], GRADE grd[], int s, int c)
{
	printf("    --进入输入模块--\n");
	int i, j;
	printf("输入科目名称:\n");
	for (j = 0; j < c; j++)
	{
		scanf_s("%s", &grd[j].Name, sizeof(grd[j].Name));
	}
	for (i = 0; i < s; i++)
	{
		stu[i].General = 0;
		printf("请输入第%d个学生学号和姓名,信息之间用空格隔开:",(i+1));
		scanf_s("%lld %s", &stu[i].ID,&stu[i].Name, sizeof(stu[i].Name));  //加上sizeof(stu[i].Name是因为要指定缓冲区大小
		for (j = 0; j < c; j++)
		{
			printf("请输入%s成绩:",grd[j].Name);
			scanf_s("%f",&stu[i].Grade[j]);
			stu[i].General = stu[i].General + stu[i].Grade[j];
		}
		stu[i].Average = stu[i].General / c;
	}
}
void Input_Excel()
{
	FILE* fp;
	errno_t err;
	if (err = fopen_s(&fp, "student_Original.csv", "w") != 0)
	{
		printf("打开文件失败");
		exit(0);
	}


}
//（2）
void Calculate(STUDENT stu[], STUDENT grd[], int s, int c)
{
	printf("    --进入计算模块--\n");
	int i, j;
	for (j = 0; j < c; j++)
	{
		grd[j].General = 0;
		for (i = 0; i < s; i++)
		{
			grd[j].General += stu[i].Grade[j];
		}
		grd[j].Average = grd[j].General / s;
	}
	for (j = 0; j < c; j++)
	{
		printf("%s  %.1f  %.1f\n", grd[j].Name,grd[j].General,grd[j].Average);
	}
}
//（10）
void Statistical(STUDENT stu[], GRADE grd[], int s, int c)
{
	printf("    --进入统计模块--\n");
	int i, j;
	for (j = 0; j < c; j++)
	{
		for (i = 0; i < s; i++)
		{
			if (stu[i].Grade[j] >= 90 && stu[i].Grade[j] <= 100)
			{
				grd[j].good++;
			}
			if (stu[i].Grade[j] >= 80 && stu[i].Grade[j] <= 89)
			{
				grd[j].lh++;
			}
			if (stu[i].Grade[j] >= 90 && stu[i].Grade[j] <= 100)
			{
				grd[j].good++;
			}
			if (stu[i].Grade[j] >= 70 && stu[i].Grade[j] <= 79)
			{
				grd[j].zd++;
			}
			if (stu[i].Grade[j] >= 60 && stu[i].Grade[j] <= 69)
			{
				grd[j].jg++;
			}
			if (stu[i].Grade[j] >= 0 && stu[i].Grade[j] <= 59)
			{
				grd[j].bad++;
			}
		}
	}
	for (j = 0; j < c; j++)
	{
		grd[j].good_pt = grd[j].good / s;
		grd[j].lh_pt = grd[j].lh / s;
		grd[j].zd_pt = grd[j].zd / s;
		grd[j].jg_pt = grd[j].jg / s;
		grd[j].bad_pt = grd[j].bad / s;
	}
	for (j = 0; j < c; j++)
	{
		printf("%ls\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n", grd[j].Name, grd[j].good_pt, grd[j].lh_pt, grd[j].zd_pt, grd[j].jg_pt, grd[j].bad_pt);
	}
}

//(11)
void Print(STUDENT stu[], GRADE grd[], int s, int c)
{
	printf("    --进入输出模块--\n");
	int i, j;
	printf("序号\t学号\t姓名\t");
	for (j = 0; j < c; j++)
	{
		printf("%s\t", grd[j].Name);
	}
	printf("总分\t平均分\t");
	printf("\n");
	for (i = 0; i < s; i++)
	{
		printf("%d.\t%lld\t%s\t",(i+1),stu[i].ID,stu[i].Name);
		for (j = 0; j < c; j++)
		{
			printf("%.1f\t", stu[i].Grade[j]);
		}
		printf("%.1f\t%.1f\n", stu[i].General, stu[i].Average);
	}
}

void Print_Excel(STUDENT stu[], GRADE grd[], int s, int c)
{
	{
		printf("    --进入文件输出模块--\n");
		int i, j;
		FILE* fp;
		char FileName[20];
		printf("请输入文件名:");
		scanf_s("%s", &FileName,sizeof(FileName));
		errno_t err;
		if (err = fopen_s(&fp, FileName, "w") != 0)
		{
			printf("打开文件失败");
			exit(0);
		}
		fprintf(fp, "Order,ID,Name,");
		for (j = 0; j < c; j++)
		{
			fprintf(fp, "%s,", grd[j].Name);
		}
		fprintf(fp, "General,Average,");
		fprintf(fp, "\n");
		for (i = 0; i < s; i++)
		{
			fprintf(fp, "%d,%lld,%s,", (i + 1), stu[i].ID, stu[i].Name);
			for (j = 0; j < c; j++)
			{
				fprintf(fp, "%.1f,", stu[i].Grade[j]);
			}
			fprintf(fp, "%.1f,%.1f,", stu[i].General, stu[i].Average);
			fprintf(fp, "\n");
		}
		fclose(fp);
	}
}
